
/**
 * 分页查询方法
 * @param url
 * @param pageNo
 * @param pageSize
 */
function submit(url, pageNo, pageSize) {
    if(pageNo === undefined){
        pageNo = initPageNo;
    }
    if(pageSize === undefined){
        pageSize = initPageSize;
    }

    layui.use(['table', 'laypage','form'], function () {
        let table = layui.table;
        let laypage = layui.laypage;
        let loading = layer.msg('正在查询...', {icon: 16, shade: 0.3, time:0});
        let requestSn = $('#requestSn').val();
        let templateNo = $('#templateNo').val();
        let templateName = $('#templateName').val();
        let sealType = $('#sealType').val();
        let isSeal = $('#isSeal').val();
        let signStatus = $('#signStatus').val();
        let callbackUrl = $('#callbackUrl').val();
        let createTime = $('#createTime').val();
        let updateTime = $('#updateTime').val();

        //第一个实例
        table.render({
            elem: '#searchTable'
            ,url: url //数据接口
            ,page: false //开启分页
            ,id:'searchTable'
            ,height: 'full-280'
            , method: 'POST'
            , headers: {"token": getCookie("token")}
            , contentType : "application/json"
            , where: {requestSn:requestSn,templateNo:templateNo,templateName:templateName,sealType:sealType,isSeal:isSeal,callbackUrl:callbackUrl,signStatus:signStatus,createTime:createTime,updateTime:updateTime,pageNo:pageNo,pageSize:pageSize}
            ,cols: [[ //表头
                {field: 'dataId',type: 'numbers',align: 'center'}
                , {field: 'requestSn', title: '流水号', width: '260', align: 'center'}
                , {field: 'templateNo', title: '模板编号', width: '180',align: 'center'}
                , {field: 'templateName', title: '模板名称', width: '200',align: 'center'}
                , {field: 'sealType', title: '签章类型', width: '100',align: 'center'}
                , {field: 'isSeal', title: '是否签章', width: '120',align: 'center'}
                , {field: 'createTime', title: '请求时间', width: '200',align: 'center',templet: function (d) {return toDateString(d.createTime);}}
                , {field: 'updateTime', title: '结束时间', width: '200', align: 'center',templet: function (d) {return toDateString(d.updateTime);}}
                , {field: 'differenceTime', title: '耗时', width: '150',align: 'center',templet: function (d) {return calculateTime(d.differenceTime);}}
                , {field: 'signStatusName', title: '处理状态', width: '100',align: 'center'}
                , {field: 'signResult', title: '处理结果', width: '150',align: 'center'}
                , {field: 'failCallNum', title: '重调次数', width: '100',align: 'center'}
                , {field: 'successCallCode', title: '回调状态',width: '100',align: 'center'}
                , {field: 'successCallMsg', title: '回调信息', width: '150',align: 'center'}
                , {field: 'callbackUrl', title: '业务系统回调地址', width: '350',align: 'center'}
                , {fixed: 'right', width: '150', title: '操作', align: 'center',toolbar: '#barSearch'} //添加工具条,
            ]]
            ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                if(res.code == 100){
                    errorMsg(res.msg);
                }else {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.pageInfo.totalCount, //解析数据长度
                        "pageNo": res.data.pageInfo.pageNo, //解析数据长度
                        "pageSize": res.data.pageInfo.pageSize, //解析数据长度
                        "data": res.data.pageInfo.list //解析数据列表
                    };
                }
            }
            ,done:function (res) {
                layer.close(loading);
                //校验登陆状态
                checkLogin(res);
                //增加自定义样式
                beautyCell();
                //自定义分页
                laypage.render({
                    elem: 'tablePage'
                    , curr: res.pageNo
                    , count: res.count
                    , limit: res.pageSize
                    ,layout: ['page', 'limit','count', 'skip']
                    ,theme: '#1b2b34'
                    ,jump: function(obj,first){
                        if(!first){
                            submit(url, obj.curr, obj.limit);
                        }
                    }
                });
            }
        });

        //工具条
        table.on('tool(searchTable)', function(obj){
            let data = obj.data;
            if(obj.event === 'paramsDetail'){
                let id = data.id;
                let createTime = data.createTime;
                let signStatus = data.signStatus;
                xadmin.open("查看合同参数","./edit.html?id="+id+"&&signStatus="+signStatus+"&&createTime="+createTime,500,650);
            }
            if(obj.event === 'lookContract'){
                //获取跳转页面地址
                let data = obj.data;
                let contractUri = data.contractUri;
                if(contractUri === null || contractUri === ""){
                    alert("该合同不存在！");
                    return false
                }
                let url = serverPre + "/manager/scs/v2/requestLog/contract/"+contractUri +"?channelNo="+data.channelNo;
                let result = requestGet(url);
                if(result === undefined){
                    alert("发生异常，请联系管理员！")
                }else {
                    if(result.contractUri !== ""){
                        window.open(result.contractUri,'_blank');
                    }else {
                        errorMsg("返回数据为空");
                    }
                }
            }
            if(obj.event === 'reSign'){
                reSign(data);
            }
        });
    });
}

/**
 * 重签
 * @param data
 */
function reSign(data) {
    createTime = toDateString(data.createTime);
    let result = requestGet(serverPre + "/manager/scs/v2/requestLog/one?id="+data.id+"&&date="+createTime)
    let url = serverPre + "/manager/signaturesync/v2/signature/repeat";

    let commitData = JSON.stringify({
        "requestSn": data.requestSn,
        "channelNo": data.channelNo,
        "templateNo": data.templateNo,
        "callBackUrl": data.callbackUrl,
        "requestData":JSON.parse(result.requestData),
        "refId":data.refId,
        "needSign": data.isSeal == '是' ? 1 : 0,
    });
    $.ajax({
        url: url,
        data: commitData,
        type: 'POST',
        contentType: 'application/json',
        headers: {"token": getCookie("token")},
        success: function (res) {
            if(res.code == 0){
                layer.msg(res.msg, {
                    icon: 1,
                    time: 3000,
                    end: function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index);
                    }
                });
            }else {
                layer.msg(res.msg, {
                    icon: 2,
                    time: 5000,
                    end: function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index);
                    }
                });
            }
        },
        error: function (err) {
            errorMsg(err);
        }
    });
}


/**
 * 美化样式
 */
function beautyCell() {
    $("[data-field='signStatus']").children().each(function (index,item) {
        // debugger
        if(item.textContent === '未处理'){
            $(this).css("color","#0000FF");
        }
        if(item.textContent === '处理中'){
            $(this).css("color","#ffff00");
        }
        if(item.textContent === '处理成功'){
            $(this).css("color","#228b22");
        }
        if(item.textContent === '处理失败'){
            $(this).css("color","#ff0000");
        }
    })
}

/**
 * 查看合同参数页面
 * @param result
 */
function buildOpenWin(result){
    if(result === undefined || result === null){
        errorMsg("返回数据为空");
    }
    if(result.channelName !== ""){
        $('#channelName').val(result.channelName);
    }
    if(result.templateNo !== ""){
        $('#templateNo').val(result.templateNo);
    }
    if(result.isSeal !== ""){
        $('#isSeal').val(result.isSeal);
    }
    if(result.callbackUrl !== ""){
        $('#callbackUrl').val(result.callbackUrl);
    }
    if(result.createTime !== ""){
        let time = toDateString(result.createTime);
        $('#createTime').val(time);
    }
    if(result.updateTime !== ""){
        let time = toDateString(result.updateTime);
        $('#updateTime').val(time);
    }
    if(result.differenceTime !== ""){
        $('#differenceTime').val(result.differenceTime);
    }
    if(result.requestData !== ""){
        $('#requestData').val(result.requestData);
    }
}

/**
 * 初始化签章状态
 */
function querySignStatus(form) {
    let url = serverPre + "/manager/scs/v2/requestLog/signStatus/list";
    let content = '<option value="">请选择</option>'; //全局变量
    let data = JSON.stringify({'pageNo':'1','pageSize':'999'});
    $.ajax({
        url: url,
        data: data,
        type: 'POST',
        contentType: 'application/json',
        headers: {'token': getCookie('token')},
        success: function (res) {
            if (res.code === 0) {
                let result = res.data.list;
                $.each(result,function (i,item) {
                    let signStatus = item.code;
                    let signStatusName = item.name;
                    content += '<option value = "' + signStatus + '">' + signStatusName + '</option>'
                });
                $("#signStatus").html(content);
                form.render('select');
            }
        },
        error: function (err) {
            errorMsg(err);
        }
    });
}

/**
 * 初始化签章类型--所有
 */
function querySealType(form) {
    let url = serverPre + "/manager/scs/v2/template/sealType/list";
    let content = '<option value="">请选择</option>'; //全局变量
    let data = JSON.stringify({'pageNo':'1','pageSize':'999'});
    $.ajax({
        url: url,
        data: data,
        type: 'POST',
        contentType: 'application/json',
        headers: {'token': getCookie('token')},
        success: function (res) {
            if (res.code === 0) {
                let result = res.data.list;
                $.each(result,function (i,item) {
                    let sealType = item.code;
                    let sealTypeName = item.name;
                    content += '<option value = "' + sealType + '">' + sealTypeName + '</option>'
                });
                $("#sealType").html(content);
                form.render('select');
            }
        },
        error: function (err) {
            errorMsg(err);
        }
    });
}
